capture log close

clear all

set more off
set matsize 4000

cd ${path}appendix

log using smog_pollution_iv.log, replace


use ../analysis/pollution_star_merged

xtset county date

/*First, divide the counts of inspections by 1000, to make the coefficients easier to read*/

gen lag = 0
foreach var of varlist firstgen* secondgen* thirdgen* othergen* old* new* {
	qui replace `var' = (`var')/1000
}


/* Generate day-of-week and calendar week*/

gen dow = dow(date)
gen week = wofd(date)
gen monthfe = ceil(month/3)

gen date2 = date^2
gen date3 = date^3

gen la = county == 19

/*Declare the "if" conditions in a local, as well as the full set of controls*/
replace monthfe = ceil(monthfe/3)
local conditions  if year>=1998 & inlist(county,5,12,18,22,26,53)==0 & (inlist(county,3,6,11,12,13,17,23,47,58)==0 | (year !=2002&year!=2003)) 
local controls tMin tMax prec ib1.county ib1.county#i.year  i.week 


/* Estadd subcommands to add F-tests of equality to the tables*/
capture program drop estadd_oldnewtest
program estadd_oldnewtest, eclass
	test firstgen = othergen
	ereturn scalar oldnew_F = r(F)
	ereturn scalar oldnew_p = r(p)
end

capture program drop estadd_mediumnewtest
program estadd_mediumnewtest, eclass
	test secondgen = thirdgen
	ereturn scalar mediumnew_F = r(F)
	ereturn scalar mediumnew_p = r(p)
	test firstgen = secondgen=thirdgen
	ereturn scalar oldnew_F=r(F)
	ereturn scalar oldnew_P=r(p)
end


/*Now run regressions for each pollutant*/

foreach pollutant in nox co{

	/*Multiply the pollutant variables by 1000 to change to ppb (again, to make coefficients readable*/

	replace `pollutant' = `pollutant'*1000
	
	/* Robustness checks for the window for our rolling totals*/
	
	forvalues i = 30(30)120 {
		eststo window_`i'_`pollutant': regress `pollutant' firstgen_`i' othergen_`i' `controls' `conditions', vce(cluster county)
	}
	
	/*Robustness checks for 90-day rolling totals:
	Exclude LA, Add day of week FE, replace county trends with countyxyear FE, collapse to weekly observations*/
	
	local counter = 1
	eststo `pollutant'_`counter++': regress `pollutant' firstgen_90 othergen_90 `controls' `conditions', vce(cluster county)
	eststo `pollutant'_`counter++': regress `pollutant' firstgen_90 othergen_90 `controls' la   `conditions' & county !=19, vce(cluster county)
	eststo `pollutant'_`counter++': regress `pollutant' firstgen_90 othergen_90  `controls' dow i.month `conditions', vce(cluster county)
		eststo `pollutant'_`counter++': regress `pollutant' firstgen_90 othergen_90  `controls' dow ib1.county#i.monthfe `conditions', vce(cluster county)
	
	preserve
	collapse `pollutant' firstgen_90 othergen_90 tMin tMax prec , by(county year week)
	gen weekly =1 //This is just to make the estout indicate option works correctly.

	eststo `pollutant'_`counter++': regress `pollutant' firstgen_90 othergen_90 tMin tMax prec ib1.county i.county#i.year i.week weekly `conditions', vce(cluster county)
	restore
	
	/* Robustness checks for grouping of vehicle vintages: break new cars into two groups */
	
	eststo age_`pollutant': regress `pollutant' firstgen_90 secondgen_90 thirdgen_90 `controls' `conditions', vce(cluster county)

	/* Instrument with a 2-year lag*/
	eststo `pollutant'_iv: reghdfe `pollutant' tMin tMax prec (firstgen_90 othergen_90 = L730.old_init_90 L730.new_init_90 L730.firstgen_90 L730.othergen_90 L730.oldpass_90 L730.newpass_90) `conditions', ffirst absorb(county week i.county#c.date i.county#c.date2 i.county#c.date3) vce(cluster county)
	
	/* County-specific trends instead of county-year FE*/
	
	eststo `pollutant'_trend1: regress `pollutant' firstgen_90 othergen_90 ib1.county i.week ib1.county#c.date  `conditions', vce(cluster county) 
	eststo `pollutant'_trend2: regress `pollutant' firstgen_90 othergen_90 ib1.county i.week ib1.county#c.date ib1.county#c.date#c.date `conditions', vce(cluster county) 
	eststo `pollutant'_trend3: regress `pollutant' firstgen_90 othergen_90  ib1.county i.week ib1.county#c.date ib1.county#c.date#c.date ib1.county#c.date#c.date#c.date `conditions', vce(cluster county) 

	
}

estadd oldnewtest: co* nox* window*
estadd mediumnewtest: age*

/* Window robustness table*/

#delimit ;

esttab window_*_co using robustness_window.tex, replace se booktabs label nonum star( * .1 ** .05 *** .01)
indicate("County FE = *.county" "Weather Controls = tMin tMax prec" "Calendar Week FE = *.week"  "County-Year FE = *.year") 
title(Re-Inspections and County-Level Daily Air Quality: Robustness Checks\label{tab:robustness-window})
varlabels(firstgen_90 "000s of Re-Inspections in Window\\ \quad\quad 1975-1985 Vehicles"  othergen_90 "\quad\quad 1985+ Vehicles")
postfoot( ) nocons noobs mgroup("A: Outcome is Carbon Monoxide (PPB)", pattern(1 0 0 0 ) span prefix(\multicolumn{@span}{c}{) suffix(}))
substitute( "\begin{tabular}" "\setlength{\linewidth}{.1cm}\newcommand{\contents}{\begin{tabular}"
            "\end{tabular}" "\end{tabular}}\setbox0=\hbox{\contents}\setlength{\linewidth}{\wd0-2\tabcolsep-.25em}\contents"
            "{l}{\footnotesize" "{p{\linewidth}}{\footnotesize")
scalars("oldnew_F F-test of Equality" "oldnew_p \$ P > F\$")
mtitles("30 Days" "60 Days" "90 days" "120 days")
rename(firstgen_30 firstgen_90 firstgen_60 firstgen_90 firstgen_120 firstgen_90
othergen_30 othergen_90 othergen_60 othergen_90 othergen_120 othergen_90)
;

esttab window_*_nox using robustness_window.tex, append se booktabs label nonum  star( * .1 ** .05 *** .01)
indicate("County FE = *.county" "Weather Controls = tMin tMax prec" "Calendar Week FE = *.week"  "County-Year FE = *.year")
mgroup("B: Outcome is NOx (PPB)", pattern(1 0 0 0 ) span prefix(\multicolumn{@span}{c}{) suffix(}))
varlabels(firstgen_90 "000s of Re-Inspections in Window\\ \quad\quad 1975-1985 Vehicles"  othergen_90 "\quad\quad 1985+ Vehicles")
prehead(\midrule )  nocons postfoot(\bottomrule @starlegend \\ \multicolumn{@span}{l}{\footnotesize @note} \end{tabular}\end{table})
note(Note: Observations are county-days.  Standard errors clustered by county reported in parentheses.)
substitute( "\begin{tabular}" "\setlength{\linewidth}{.1cm}\newcommand{\contents}{\begin{tabular}"
            "\end{tabular}" "\end{tabular}}\setbox0=\hbox{\contents}\setlength{\linewidth}{\wd0-2\tabcolsep-.25em}\contents"
            "{l}{\footnotesize" "{p{\linewidth}}{\footnotesize")
scalars("oldnew_F F-test of Equality" "oldnew_p \$ P > F\$")
mtitles("30 Days" "60 Days" "90 days" "120 days")
rename(firstgen_30 firstgen_90 firstgen_60 firstgen_90 firstgen_120 firstgen_90
othergen_30 othergen_90 othergen_60 othergen_90 othergen_120 othergen_90)
;


/* Specification Robustness Table:*/
#delimit ;

esttab co_1 co_2 co_3 co_4 co_5  using robustness.tex, replace se booktabs label nomtitles star( * .1 ** .05 *** .01) drop(_cons *.county *.week tMin tMax prec *.year)
indicate(/*"County FE = *.county"  "Calendar Week FE = *.week"  "Weather Controls = tMin tMax prec"  "County-Year FE = *.year"*/ "Exclude LA = *la*"
"Month-of-Year and Day-of Week FE = *.month *dow"   "County-Season FE = *monthfe" "Weekly Data = weekly") title(Re-Inspections and County-Level Daily Air Quality: Robustness Checks\label{tab:robustness})
varlabels(firstgen_90 "000s of Re-Inspections Last 90 Days\\ \quad\quad 1975-1985 Vehicles"  othergen_90 "\quad\quad 1985+ Vehicles")
postfoot( ) nocons noobs mgroup("A: Outcome is Carbon Monoxide (PPB)", pattern(1 0 0 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}))
substitute( "\begin{tabular}" "\setlength{\linewidth}{.1cm}\newcommand{\contents}{\begin{tabular}"
            "\end{tabular}" "\end{tabular}}\setbox0=\hbox{\contents}\setlength{\linewidth}{\wd0-2\tabcolsep-.25em}\contents"
            "{l}{\footnotesize" "{p{\linewidth}}{\footnotesize")
scalars("oldnew_F F-test of Equality" "oldnew_p \$ P > F\$")	    
;

esttab nox_1 nox_2 nox_3 nox_4 nox_5 using robustness.tex, append se booktabs label nomtitles  star( * .1 ** .05 *** .01)
indicate(/*"County FE = *.county"  "Calendar Week FE = *.week"  "Weather Controls = tMin tMax prec"  "County-Year FE = *.year"*/ "Exclude LA = *la*"
"Month-of-Year and Day-of Week FE = *.month *dow"    "County-Season FE = *monthfe" "Weekly Data = weekly")
mgroup("B: Outcome is NOx (PPB)", pattern(1 0 0 0 0) span prefix(\multicolumn{@span}{c}{) suffix(})) drop(_cons *.county *.week tMin tMax prec *.year)
varlabels(firstgen_90 "000s of Re-Inspections Last 90 Days\\ \quad\quad 1975-1985 Vehicles"  othergen_90 "\quad\quad 1985+ Vehicles")
prehead(\midrule )  nocons postfoot(\bottomrule @starlegend \\ \multicolumn{@span}{l}{\footnotesize @note} \end{tabular}\end{table})
note(Note: Observations are county-days.  All regressions include county fixed effects, calendar week fixed effects, controls for weather and county-by-year fixed effects.  Standard errors clustered by county reported in parentheses.)
substitute( "\begin{tabular}" "\setlength{\linewidth}{.1cm}\newcommand{\contents}{\begin{tabular}"
            "\end{tabular}" "\end{tabular}}\setbox0=\hbox{\contents}\setlength{\linewidth}{\wd0-2\tabcolsep-.25em}\contents"
            "{l}{\footnotesize" "{p{\linewidth}}{\footnotesize")
scalars("oldnew_F F-test of Equality" "oldnew_p \$ P > F\$")	    
;

/* Parametric trends robustness table */

esttab co_1 co_trend*  using robustness_trend.tex, replace se booktabs label nomtitles star( * .1 ** .05 *** .01)
indicate("County FE = *.county"  "Calendar Week FE = *.week"  "Weather Controls = tMin tMax prec"  "County-Year FE = *.year" 
"County Linear Trends = *y#c.date" "County Quadratic Trends = *y#c.date#c.date" "County Cubic Trends = *y#c.date#c.date#c.date") title(Re-Inspections and County-Level Daily Air Quality: Using Parametric County-Specific Trends\label{tab:robustness-trends})
varlabels(firstgen_90 "000s of Re-Inspections Last 90 Days\\ \quad\quad 1975-1985 Vehicles"  othergen_90 "\quad\quad 1985+ Vehicles")
postfoot( ) nocons noobs mgroup("A: Outcome is Carbon Monoxide (PPB)", pattern(1 0 0 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}))
substitute( "\begin{tabular}" "\setlength{\linewidth}{.1cm}\newcommand{\contents}{\begin{tabular}"
            "\end{tabular}" "\end{tabular}}\setbox0=\hbox{\contents}\setlength{\linewidth}{\wd0-2\tabcolsep-.25em}\contents"
            "{l}{\footnotesize" "{p{\linewidth}}{\footnotesize")
scalars("oldnew_F F-test of Equality" "oldnew_p \$ P > F\$")	    
;

esttab nox_1 nox_trend* using robustness_trend.tex, append se booktabs label nomtitles  star( * .1 ** .05 *** .01)
indicate("County FE = *.county"  "Calendar Week FE = *.week"  "Weather Controls = tMin tMax prec"  "County-Year FE = *.year" 
"County Linear Trends = *y#c.date" "County Quadratic Trends = *y#c.date#c.date" "County Cubic Trends = *y#c.date#c.date#c.date")
mgroup("B: Outcome is NOx (PPB)", pattern(1 0 0 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}))
varlabels(firstgen_90 "000s of Re-Inspections Last 90 Days\\ \quad\quad 1975-1985 Vehicles"  othergen_90 "\quad\quad 1985+ Vehicles")
prehead(\midrule )  nocons postfoot(\bottomrule @starlegend \\ \multicolumn{@span}{l}{\footnotesize @note} \end{tabular}\end{table})
note(Note: Observations are county-days.  Standard errors clustered by county reported in parentheses.)
substitute( "\begin{tabular}" "\setlength{\linewidth}{.1cm}\newcommand{\contents}{\begin{tabular}"
            "\end{tabular}" "\end{tabular}}\setbox0=\hbox{\contents}\setlength{\linewidth}{\wd0-2\tabcolsep-.25em}\contents"
            "{l}{\footnotesize" "{p{\linewidth}}{\footnotesize")
scalars("oldnew_F F-test of Equality" "oldnew_p \$ P > F\$")	    
;

/* Age robustness table*/

esttab age_co age_nox using robustness_age.tex, replace se booktabs label  star( * .1 ** .05 *** .01)
indicate("County FE = *.county" "Weather Controls = tMin tMax prec" "Calendar Week FE = *.week"  "County-Year FE = *.year") title(Re-Inspections and County-Level Daily Air Quality: Robustness Checks\label{tab:robustness-age})
varlabels(firstgen_90 "000s of Re-Inspections Last 90 Days\\ \quad\quad 1975--1985 Vehicles" secondgen_90 "\quad\quad 1985-1995 Vehicles"  thirdgen_90 "\quad\quad 1995+ Vehicles")
 nocons  mtitles(CO \nox) nonum
substitute( "\begin{tabular}" "\setlength{\linewidth}{.1cm}\newcommand{\contents}{\begin{tabular}"
            "\end{tabular}" "\end{tabular}}\setbox0=\hbox{\contents}\setlength{\linewidth}{\wd0-2\tabcolsep-.25em}\contents"
            "{l}{\footnotesize" "{p{\linewidth}}{\footnotesize")
scalars("mediumnew_F F(1985--1996=1996+)" "oldnew_F F(All three equal)")
;

/* IV robustness table */

#delimit ;
esttab co_iv* nox_iv* using robustness_iv.tex, replace se booktabs label  star( * .1 ** .05 *** .01) keep(firstgen_90 othergen_90)
/*indicate("County FE = *.county" "Weather Controls = tMin tMax prec" "Calendar Week FE = *.week" "County Cubic Trends= *date")*/ title(Re-Inspections and County-Level Daily Air Quality: Instrumenting with 2-year lags\label{tab:robustness-iv})
varlabels(firstgen_90 "000s of Re-Inspections Last 90 Days\\ \quad\quad 1975--1985 Vehicles" othergen_90 "\quad\quad 1985+ Vehicles"
c.fpr_current_firstgen_90#c.firstgen_90 "\quad\quad 1975-1985 Vehicles \$\cdot\$ Station Quality" 
c.fpr_current_othergen_90#c.othergen_90 "\quad\quad 1985+ Vehicles \$\cdot\$ Station Quality")
addnote("Note: Observations are county-days.  Standard errors clustered by county reported in parentheses.  F-tests of equality report the F-statistics from testing the null hypothesis that the coefficient on 1975--1985 model year vehicles equals the coefficient on 1985+ model year vehicles.  All regressions instrument for re-inspections with the 730-day lag of initial-inspections, initial passed inspections, and re-inspections of 1975--1985 model year vehicles and 1985+ model year vehicles." )
 nocons mtitles(CO \nox) nonum nonote
substitute( "\begin{tabular}" "\setlength{\linewidth}{.1cm}\newcommand{\contents}{\begin{tabular}"
            "\end{tabular}" "\end{tabular}}\setbox0=\hbox{\contents}\setlength{\linewidth}{\wd0-2\tabcolsep-.25em}\contents"
            "{l}{\footnotesize" "{p{\linewidth}}{\footnotesize")
scalars("rkf First-Stage F-statistic" "oldnew_F F-test of Equality" "oldnew_p \$ P > F\$")	  

;

#delimit cr

instanttex using robust_tables.tex, replace table(robustness_window.tex robustness.tex robustness_age.tex robustness_iv.tex) p(calc) preamble("\newcommand{\nox}{NOx}")
